package com.server.llh.llhserverwork.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.llh.llhsuppliers.R;
import com.server.llh.llhserverwork.activity.server.LlhSubscribeActivity;
import com.server.llh.llhserverwork.bean.OldManInfo;
import com.server.llh.llhserverwork.bean.SerStockDetailWapBean;
import com.server.llh.llhserverwork.bean.SerStockWapBean;
import com.server.llh.llhserverwork.bean.StockDetailPage;
import com.server.llh.llhserverwork.utils.ToastTool;
import com.server.llh.llhserverwork.view.AddListView;

import java.util.List;


/**
 * 查询服务预约信息
 *
 * @author lelaohui
 */

public class ServerOrderInfosAdapter extends BaseAdapter {
    List<StockDetailPage> listData;
    boolean onClickA = false;

    public List<StockDetailPage> getListData() {
        return listData;
    }

    public void setListData(List<StockDetailPage> listData) {
        this.listData = listData;
    }

    private Context context;
    OldManInfo userId;

    public ServerOrderInfosAdapter(Context context,
                                   List<StockDetailPage> listData, OldManInfo userId) {
        this.context = context;
        this.listData = listData;
        this.userId = userId;
    }

    @Override
    public int getCount() {
        if (listData == null || listData.size() == 0) {
            return 0;
        }
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.query_clien_server_item, parent, false);
            holder = new ViewHolder();
            holder.order_code = (TextView) convertView
                    .findViewById(R.id.order_code);
            holder.order_time = (TextView) convertView
                    .findViewById(R.id.order_time);
            holder.stockList = (AddListView) convertView
                    .findViewById(R.id.stockList);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        StockDetailPage infoData = listData.get(position);
        holder.order_code.setText("订单号:" + infoData.getOrderId());
        if (infoData.getOrderDate() != null) {
            holder.order_time.setText("时间" + infoData.getOrderDate());
        }
        StockListAdapter adapter = new StockListAdapter(infoData.getStockList());
        holder.stockList.setAdapter(adapter);
        return convertView;
    }

    class ViewHolder {
        TextView order_code, order_time;
        AddListView stockList;
    }

    class StockListAdapter extends BaseAdapter {
        List<SerStockWapBean> stockList;

        public StockListAdapter(List<SerStockWapBean> stockList) {
            this.stockList = stockList;
        }

        @Override
        public int getCount() {
            if (stockList == null || stockList.size() == 0) {
                return 0;
            }
            return stockList.size();
        }

        @Override
        public Object getItem(int position) {
            return stockList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            StockHolder holder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(
                        R.layout.server_stock_detail_list, parent, false);
                holder = new StockHolder();
                holder.gift_packs = (TextView) convertView
                        .findViewById(R.id.gift_packs);
                holder.detail_list = (AddListView) convertView
                        .findViewById(R.id.detail_list);
                convertView.setTag(holder);
            } else {
                holder = (StockHolder) convertView.getTag();
            }
            SerStockWapBean infoData = stockList.get(position);
            if (infoData.getPackageName() != null) {
                holder.gift_packs.setText("" + infoData.getPackageName());
            } else {
                holder.gift_packs.setText("出错了");
            }
            DetailListAdapter adapter = new DetailListAdapter(
                    infoData.getSerStockDetailList());
            holder.detail_list.setAdapter(adapter);
            return convertView;
        }

    }

    class StockHolder {
        TextView gift_packs;
        AddListView detail_list;
    }

    class DetailListAdapter extends BaseAdapter {
        List<SerStockDetailWapBean> serStockDetailList;

        public DetailListAdapter(List<SerStockDetailWapBean> serStockDetailList) {
            this.serStockDetailList = serStockDetailList;
        }

        @Override
        public int getCount() {
            if (serStockDetailList == null || serStockDetailList.size() == 0) {
                return 0;
            }
            return serStockDetailList.size();
        }

        @Override
        public Object getItem(int position) {
            return serStockDetailList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView,
                            ViewGroup parent) {
            DetailHolder holder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(
                        R.layout.stock_detail_list_item, parent, false);
                holder = new DetailHolder();
                holder.server_content = (TextView) convertView
                        .findViewById(R.id.server_content);
                holder.server_content_num = (TextView) convertView
                        .findViewById(R.id.server_content_num);
                holder.server_surplus_num = (TextView) convertView
                        .findViewById(R.id.server_surplus_num);
                holder.server_rule_content = (TextView) convertView
                        .findViewById(R.id.server_rule_content);
                holder.server_time_content = (TextView) convertView
                        .findViewById(R.id.server_time_content);
                holder.btn_uplode = (TextView) convertView
                        .findViewById(R.id.btn_uplode);
                holder.endTime = (TextView) convertView
                        .findViewById(R.id.endTime);

                convertView.setTag(holder);
            } else {
                holder = (DetailHolder) convertView.getTag();
            }
            final SerStockDetailWapBean infoData = serStockDetailList.get(position);
            if (infoData.getEndDateStr() != null && "".equals(infoData.getEndDateStr())) {
                holder.endTime.setText("库存截止时间:" + infoData.getEndDateStr());
            }
            holder.server_content.setText("" + infoData.getServiceName());
            holder.server_content_num.setText("总:" + infoData.getTotalNum() + "次");
            holder.server_surplus_num.setText("剩:" + infoData.getCurrentNum() + "次");
            holder.server_rule_content.setText("" + infoData.getRuleStr());
            holder.server_time_content.setText("" + infoData.getExecDates());
            int isEnable = infoData.getIsEnable();
            if (isEnable != 1) {
                holder.btn_uplode.setText("不可预约");
            } else if (infoData.getCurrentNum() == 0) {
                holder.btn_uplode.setText("不可预约");
            } else {
                holder.btn_uplode.setText("预约");
            }
            Log.d("tag===", "onClickAdddd==" + onClickA);

            holder.btn_uplode.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (onClickA == false) {
                        onClickA = true;
                        if (infoData.getIsEnable() != 1) {
                            ToastTool.showText(context, "抱歉,该服务项不可预约");
                        } else if (infoData.getCurrentNum() == 0) {
                            ToastTool.showText(context, "抱歉,该服务项不可预约");
                        } else {
                            Log.d("tag===", "onClickAbbb==" + onClickA);
                            Intent intent = new Intent(context,
                                    LlhSubscribeActivity.class);
                            intent.putExtra("infoData", infoData);
                            intent.putExtra("customerId", userId);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            context.startActivity(intent);
                        }
                    } else {
                        ToastTool.showText(context, "预约正在进行中，请稍后再试！");
                    }
                }
            });


            return convertView;
        }
    }

    class DetailHolder {
        TextView btn_uplode;
        TextView server_content, endTime, server_time_content, server_content_num,
                server_surplus_num, server_rule_content;
    }

}
